const Web3 = require('web3')
const Token = require('../Token.json')

const ganacheAddress = '0xFBef33e09081236cC4A1f8D2C175E9fEf6Aa2DFa'
const userAddress = '0xac9199717985cDaC98e5832370B208B8a33A50Ef'

// 连接provider
let local_provider = new Web3.providers.HttpProvider('http://localhost:8545')
let naturelab_provider = new Web3.providers.HttpProvider(
  'http://47.243.92.131:8545',
)

// 合约
const abi = Token.abi
const bytecode = Token.bytecode

const formatUnits = (value) => {
  return Web3.utils.fromWei(value, 'mwei')
}

async function getTokenBalance(
  contractAddress,
  userAddress,
  provider,
  enviroment,
) {
  // 连接provider
  var web3 = new Web3(provider)

  var contractInstance = new web3.eth.Contract(abi, contractAddress)

  let balance = ''
  await contractInstance.methods
    .balanceOf(userAddress)
    .call()
    .then(function (result) {
      balance = result
      console.log(`${enviroment}====>balance:`, formatUnits(balance))
    })
  return balance
}

const USDT_mainnet = '0xdac17f958d2ee523a2206206994597c13d831ec7'
const USDT_natruelab = '0xb35DC76A99d9D7cb5c427a84349AdbE48757b7ac'

getTokenBalance(USDT_mainnet, ganacheAddress, local_provider, '本地环境')

getTokenBalance(USDT_natruelab, userAddress, naturelab_provider, 'natruelab')
